﻿#include<cstdlib>
#include<iostream>
#include <memory>
using namespace std;

class Leifeng
{
public:
	void Sweep()
	{
		cout<<"Sweep"<<endl;
	}
	void Wash()
	{
		cout<<"Wash"<<endl;
	}
	void BuyRice()
	{
		cout<<"Buy Rice"<<endl;
	}

};
class UnderGraduate: public Leifeng
{

};
class Voluteer : public Leifeng
{

};
//简单工厂模式
class StaticFactory
{
public:
	static Leifeng* CreateLeifeng(int type)//违反了开放-闭包原则
	{
		Leifeng * lp  = NULL;
		switch(type)
		{
		case 1:
			lp = new UnderGraduate();
			break;
		case 2:
			lp = new Voluteer();
			break;
		default:
			break;
		}
		return lp;
	}

};
//工厂方法
class IFactory
{
public:
	virtual Leifeng* Createleifeng() = 0;
	
};
class UnderGraduateFactory : public IFactory
{
public:
	Leifeng* Createleifeng() {
		Leifeng* lp = new Voluteer();
		return lp;
	}
};

class VoluteerFactory : public IFactory
{
public:
	Leifeng* Createleifeng() {
		Leifeng* lp = new Voluteer();
		return lp;
	}
};
int main()
{
	//简单工厂
	//Client需要的信息少
	StaticFactory lSF;
	Leifeng* lp1 = lSF.CreateLeifeng(1);
	if (lp1)
	{
		lp1->BuyRice();
		lp1->Sweep();
		lp1->Wash();
		delete lp1;
	}
	
	//工厂方法
	//Client要控制类的生成
	IFactory* lpFactory = new UnderGraduateFactory();
	if (lpFactory)
	{
		Leifeng * lp= lpFactory->Createleifeng();
		if (lp)
		{
			lp->BuyRice();
			lp->Sweep();
			lp->Wash();
			delete lp;
		}
	}
	
	getchar();
	getchar();
	return 0;
}